from web import template
render = template.render('templates/')

def filelist(files):
    result = ""
    files.sort(key=lambda obj:obj.path)
    last = 0
    for f in files:
        i = 0
        while i < f.depth-1:
            if not last or f.path[i] != last.path[i]:
                result += str(render.files_dir(f.path[i], i+1))
                last = 0
            i += 1
        result += str(render.file(f))
        last = f
    return result
template.Template.globals['filelist'] = filelist
